<?php echo $this->render('frontend_common/header.phtml'); ?>
<div>
<div id="Content">
<div style="width: 1000px; float: left; margin-left: 60px">
	<p>
	<?php 
		$hoa_table=new Application_Model_Hoa();
		$hoa_data=$hoa_table->fetchAll()->toArray();
	?>
	<form name="CTgiohoa" action="addflower" method="post">
		<h2>THÔNG TIN GIỎ HOA</h2>
		<table>
			<tr>
				<td>Loại giỏ hoa</td>
				<td>
					<select name="loai_id">
						<?php
							$loai_data= new Application_Model_LoaiGioHoa();
									$loai_table=$loai_data->fetchAll()->toArray();
									foreach ($loai_table as $value)
									{
										?>
										<option value="<?php echo $value['id'] ?>"><?php echo $value['tenloai'] ?></option>
								<?php 
									} 
					 			?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Tên giỏ hoa : </td>
				<td>
					<input required="required" type="text" name="tengiohoa">
				</td>
			</tr>
			<tr>
				<td>Đơn giá bán :</td>
				<td>
					<input required="required" type="text" name="dongiaban">
				</td>
			</tr>
			<tr>
				<td>Số lượng tồn :</td>
				<td>
					<input required="required" type="text" name="slton">
				</td>
			</tr>
			<tr>
				<td>Tên file ảnh :</td>
				<td>
					<input required="required" type="text" name="img_url">
				</td>
			</tr>
		</table>
		<h2>CHI TIẾT GIỎ HOA</h2>
		<table>
			<tr style="border: 1px solid;">
				<th>Tên hoa</th>
				<th>Số lượng</th>
				<th>Giá</th>
			</tr>
			<?php
				foreach ($hoa_data as $mothoa)
				{ 
			 ?>
			 		<tr id="<?php echo $mothoa['id'] ?>">
			 			<td><?php echo $mothoa['tenhoa'] ?></td>
			 			<td>
			 				<input type="text" name="<?php echo $mothoa['id'] ?>" class="soluong" value="0"/>
			 			</td>
			 			<td align="right">
			 				<label class="giahoa">0</label>
			 			</td>
			 		</tr>
			 <?php 
				}
			 ?>
			 <tr>
			 	<td colspan="2" align="right">
			 		Tổng tiền hoa là :
		 		</td>
		 		<td align="right">
		 			<input disabled="disabled" style="text-align: right;" readonly="readonly" type="text" name="tongtienhoa" id="tongtienhoa" value="0">
		 		</td>
			 </tr>
			 
		</table>
		<table style="background-color: rgba(255,255,255,0)">
			<tr>
			 	<td colspan="2" align="right">
			 		<input type="reset" value="reset" style="padding: 10px; width: 80px">
			 		<input type="submit" value="Thêm" style="padding: 10px; width: 100px">
			 	</td>
			 </tr>
		</table>
	</form>
	</p>
</div>


<script type="text/javascript">
	$(document).ready(function(){
		$('.soluong').keyup(function(){//Khi thay đổi giá trị trong textbox số lượng
			var id = $(this).parent().parent().attr('id');
			var giacu=$('#'+id +' .giahoa').html();
			giacu=parseInt(giacu);
			var soluong = $(this).val();
			console.log(soluong);
			
			//soluong  = parseInt(soluong);
			//alert(soluong);

			if(soluong >1000){
				$(this).val('1000');
				soluong = 1000;
			}else if(soluong <0  || isNaN(soluong)){
				$(this).val('0');
				soluong = 0;
			}

			
			var parent = $(this).parent().parent();
			var id_hoa = parent.attr('id');
			var data = "";
			data += '&hoa_id='+id_hoa+'&slhoa='+soluong;
			$.ajax({
		           url :'<?php echo $this->baseUrl('/admin/tinhtientonghoa'); ?>',
		           data : data,
		           dataType :"json",
		           type : 'POST',
		           success : function(result){
			           //Cập nhật lại price
			           $('#'+id_hoa +' .giahoa').html(result);

			           //Cập nhật lại tổng tiền
			           var tongtien=$('#tongtienhoa').val();
			           tongtien=parseInt(tongtien);
			           tongtien=tongtien-giacu+result;
			           $('#tongtienhoa').val(tongtien);
		           }
		      	});

		});
	});
</script>
<?php echo $this->render('frontend_common/footer.phtml'); ?>